#include "../../include/Dict/ChDictionary.h"
CHDictionary ::CHDictionary()
:_jieba(setjieba()){
    std::ios::sync_with_stdio(false);
    _dir="../data/cn_art"; 
       readStopFile("../data/stopWord.txt");
        getFiles();
        auto it=_file.begin();
         while(it!=_file.end()){
          //cout<<"begin"<<endl;
          readFile(*it);
          it++;
          //cout<<"success"<<endl;
        }
        writeIndex("../data/IndexFile.txt");
        writeFile("../data/WordCount.txt");

}
void CHDictionary ::readFile(const string &filename){
      ifstream ifs(filename);
       if(!ifs)
        {   
            cout << "ifstream open " << filename << " error!" << endl;
            return;
        }
        string line;
        size_t lineNumber = 0;
       while(getline(ifs, line))
        {
            //_file.push_back(line);
            jiebaWord(_jieba,line,lineNumber);
        }
        ifs.close();
}
void CHDictionary ::jiebaWord(cppjieba::Jieba &jieba,string &line
                           ,size_t &lineNumber)
{
    vector<string> words;
     jieba.CutForSearch(line,words,true);
     for(auto &word:words){
         auto it=_stop_word.find(word);
         if(it==_stop_word.end()){
             if(word!=" "&&!isdigit(word[0])&&!isalpha(word[0])){
                ++_dict[word];
                _wordline[word].insert(lineNumber);
           
             }
         }
      }
     lineNumber++;
}
void CHDictionary ::readStopFile(const string &filename){
        ifstream ifs1(filename);
        if(!ifs1){                                                      
            cout<<"error"<<endl;
            return;
        }
        string word;
        while(ifs1>>word){
                _stop_word.insert(word);
            }
        ifs1.close();
}
void CHDictionary ::writeFile(const string &filename){
    ofstream ofs(filename);
    for(auto &e:_dict){ ofs<<e.first<<" "<<e.second<<endl;
    }
    cout<<endl;
}
void CHDictionary ::writeIndex(const string &filename){
    ofstream ofs(filename);
    for(auto &e:_wordline){
        ofs<<e.first<<"     ";
        for(auto &f:e.second){
            ofs<<f+1<<" ";
        }
        ofs<<endl;
    }
}
void CHDictionary ::getFiles(){
    DIR *pdir=opendir(_dir.c_str());
    if(!pdir){
        perror("opendir");
        return;
    }
    struct dirent *ptr;
    while((ptr=readdir(pdir))!=0){
        if(strcmp(ptr->d_name,".")!=0&&strcmp(ptr->d_name,"..")!=0){
            _file.push_back(_dir+"/"+ptr->d_name);
        }
    }
    closedir(pdir);
}

int main()
{
    CHDictionary dict;
}
